package com.lxu.service;

import com.lxu.entity.ReportDataEntity;
import com.lxu.entity.ReportDirEntity;
import com.lxu.entity.ReportExportConfigEntity;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.ColumnMapRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Service;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

/**
 * 模板查詢服務
 */
@Service("wew1")
public class TemplateQueryService {

    @Autowired
    private JdbcTemplate jdbcTemplate;

    /**
     * 查看某個項目下面的某個客戶对应的某个模板类型的报表模板目录
     *
     * @param reportData 模板报表
     * @return
     */
    public List<ReportDirEntity> queryReportDirs(ReportDataEntity reportData) {
        String strSql = "SELECT rr.ID,rr.DATA_SQL,rr.SORT,rr.DATA_TYPE,rr.TEMPLATE_FILE_PATH,rr.DEST_FILE_PATH" +
                " FROM research_report_data rd,research_report_dir rr" +
                " WHERE rd.ID=rr.TEMPLATE_ID AND rd.PROJECT_ID=? AND rd.CUST_ID=?" +
                " AND rd.TEMPLATE_TYPE=? ORDER BY rr.SORT";
        List<ReportDirEntity> dirs = jdbcTemplate.query(strSql, new Object[]{reportData.getProjectId(),
                reportData.getCustomerId(), reportData.getTemplateType()}, new RowMapper<ReportDirEntity>() {
            @Override
            public ReportDirEntity mapRow(ResultSet rs, int rowNum) throws SQLException {
                ReportDirEntity reportDirEntity = new ReportDirEntity();
                reportDirEntity.setId(rs.getString(1));
                reportDirEntity.setDataSql(rs.getString(2));
                reportDirEntity.setSort(rs.getInt(3));
                reportDirEntity.setDataType(rs.getString(4));
                reportDirEntity.setTemplateFilePath(rs.getString(5));
                reportDirEntity.setDestFilePath(rs.getString(6));
                return reportDirEntity;
            }
        });
        return dirs;
    }

    /**
     * 根据目录ID获取需要导出的配置
     *
     * @param id
     * @return
     */
    public List<ReportExportConfigEntity> queryDataByDir(String id) {
        String strSql = "SELECT t.DATA_SQL,t.START_POSITION,t.TABLE_INDEX,t.META_TYPE,t.MIN_ROWS " +
                " FROM research_report_exp_config t WHERE t.DIR_ID=? /*ORDER BY t.SORT*/";
        List<ReportExportConfigEntity> exps = jdbcTemplate.query(strSql, new Object[]{id}, new RowMapper<ReportExportConfigEntity>() {
            @Override
            public ReportExportConfigEntity mapRow(ResultSet rs, int rowNum) throws SQLException {
                ReportExportConfigEntity result = new ReportExportConfigEntity();
                result.setDataSql(rs.getString(1));
                result.setStartPosition(rs.getInt(2));
                result.setTableIndex(rs.getInt(3));
                result.setMetaType(rs.getString(4));
                result.setMinRows(rs.getInt(5));
                return result;
            }
        });
        return exps;
    }

    /**
     * 查询当前项目下用户的报告模板信息
     *
     * @param entity
     * @return
     */
    public ReportDataEntity queryReportData(ReportDataEntity entity) {
        String strSql = "SELECT t.ID,t.PROJECT_ID,t.CUST_ID,t.TEMPLATE_TYPE,t.REPORT_ADDR" +
                " FROM research_report_data t WHERE t.PROJECT_ID=? AND t.CUST_ID=?" +
                " AND t.TEMPLATE_TYPE=?  ";
        List<ReportDataEntity> reports = jdbcTemplate.query(strSql, new Object[]{entity.getProjectId(),
                entity.getCustomerId(), entity.getTemplateType()}, new RowMapper<ReportDataEntity>() {
            @Override
            public ReportDataEntity mapRow(ResultSet rs, int rowNum) throws SQLException {
                ReportDataEntity result = new ReportDataEntity();
                result.setId(rs.getString(1));
                result.setProjectId(rs.getString(2));
                result.setCustomerId(rs.getString(3));
                result.setTemplateType(rs.getString(4));
                result.setReportAddr(rs.getString(5));
                return result;
            }
        });
        return CollectionUtils.isEmpty(reports) ? null : reports.get(0);
    }
}
